<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar> </template>

      <template #isUsing="{ record }">
        <Tag color="green" v-if="record.isUsing === 'Y'"> 启用 </Tag>
        <Tag color="red" v-else> 禁用 </Tag>
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              auth: 'ScheduleManagerPriv.Run',
              icon: 'codicon:run-all',
              tooltip: '立即执行',
              onClick: handleExcuted.bind(null, record),
            },
            {
              auth: 'ScheduleManagerPriv.Edit',
              disabled: record.fixed,
              icon: 'clarity:note-edit-line',
              tooltip: '编辑',
              onClick: handleEdit.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <ScheduleModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getScueduleList, excutedSchedule } from '/@/api/system/system';

  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import ScheduleModal from './ScheduleModal.vue';
  import { Tag } from 'ant-design-vue';

  import { columns, searchFormSchema } from './schedule.data';

  export default defineComponent({
    name: 'ScheduleManager',
    components: {
      BasicTable,
      TableAction,
      ScheduleModal,
      Tag,
    },
    setup() {
      const { createMessage } = useMessage();
      const [registerTable, { reload }] = useTable({
        title: '定时任务列表',
        api: getScueduleList,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

      function handleSuccess() {
        reload();
      }

      function handleExcuted(record: Recordable) {
        excutedSchedule({ id: record.id }).then(() => {
          createMessage.success('任务开始执行');
        });
      }

      /***************************Modal***********************************/

      const [registerModal, { openModal }] = useModal();

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      return {
        registerTable,
        handleEdit,
        handleSuccess,
        handleExcuted,
        registerModal,
      };
    },
  });
</script>
